﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Common
{
    public class RegistryUtility
    {
        /// <summary>
        /// 判断路径是否存在，路径为software下相对路径
        /// </summary>
        /// <param name="path">路径为software下相对路径</param>
        /// <returns></returns>
        public static bool IsPathExist(string path)
        {
            try
            {
                using (RegistryKey hkml = Registry.LocalMachine)
                {
                    RegistryKey software = hkml.OpenSubKey("software\\" + path, true);
                    if (software == null)
                    {
                        return false;
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("RegistryUtility-IsPathExist:path-" + path + ";错误信息:" + ex.Message);
            }
            return false;
        }

        /// <summary>
        /// 创建路径
        /// </summary>
        /// <param name="Path">路径为software下相对路径</param>
        public static void CreatePath(string path)
        {
            try
            {
                using (RegistryKey hkml = Registry.LocalMachine)
                {
                    RegistryKey software = hkml.OpenSubKey("software\\" + path, true);
                    if (software == null)
                    {
                        hkml.CreateSubKey("software\\" + path, RegistryKeyPermissionCheck.ReadWriteSubTree);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("RegistryUtility-CreatePath:path-" + path + ";错误信息:" + ex.Message);
            }
        }

        /// <summary>
        /// 获取路径下的子路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> GetSubPath(string path)
        {
            List<string> subPathList = new List<string>();
            try
            {
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey("software\\" + path, true);
                if (software == null)
                {
                    Logger.Warning("RegistryUtility-GetSubPath:path-" + path + ";路径不存在");
                    return subPathList;
                }
                foreach(string subpath in software.GetSubKeyNames())
                {
                    subPathList.Add(subpath);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("RegistryUtility-GetSubPath:path-" + path + ";错误信息:" + ex.Message);
            }
            return subPathList;
        }

        /// <summary>
        /// 写注册表 string类型
        /// </summary>
        /// <param name="path">路径为software下相对路径</param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void WriteString(string path, string key, string value)
        {
            try
            {
                using (RegistryKey hkml = Registry.LocalMachine)
                {
                    RegistryKey software = hkml.OpenSubKey("software\\" + path, true);
                    if (software == null)
                    {
                        software = hkml.CreateSubKey("software\\" + path, RegistryKeyPermissionCheck.ReadWriteSubTree);
                    }
                    software.SetValue(key, value, RegistryValueKind.String);
                    software.Close();
                    software.Dispose();

                    Logger.Info("RegistryUtility-WriteString:path-" + path + ";key-" + key + ";value-" + value + ";写入成功");
                }
            }
            catch (Exception ex)
            {
                Logger.Error("RegistryUtility-WriteString:path-" + path + ";key-" + key + ";value-" + value + ";错误信息:" + ex.Message);
            }
        }

        /// <summary>
        /// 读取注册表 string类型
        /// </summary>
        /// <param name="path">路径为software下相对路径</param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ReadString(string path, string key)
        {
            try
            {
                string ret = string.Empty;
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey("software\\" + path, true);
                if (software == null)
                {
                    Logger.Warning("RegistryUtility-ReadString:path-" + path + ";key-" + key + ";路径不存在");
                    return ret;
                }
                if (software.GetValueNames().Contains(key))
                {
                    ret = software.GetValue(key).ToString();
                }
                else
                {
                    Logger.Warning("RegistryUtility-ReadString:path-" + path + ";key-" + key + ";不存在该键");
                    return ret;
                }

                Logger.Info("RegistryUtility-ReadString:path-" + path + ";key-" + key + ";读取成功");
                return ret;
            }
            catch (Exception ex)
            {
                Logger.Error("RegistryUtility-ReadString:path-" + path + ";key-" + key + ";错误信息:" + ex.Message);
                return "";
            }
        }

        /// <summary>
        ///  写注册表 byte类型
        /// </summary>
        /// <param name="path">路径为software下相对路径</param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void WriteByte(string path, string key, byte value)
        {
            try
            {
                using (RegistryKey hkml = Registry.LocalMachine)
                {
                    RegistryKey software = hkml.OpenSubKey("software\\" + path, true);
                    if (software == null)
                    {
                        software = hkml.CreateSubKey("software\\" + path, RegistryKeyPermissionCheck.ReadWriteSubTree);
                    }
                    software.SetValue(key, value, RegistryValueKind.DWord);
                    software.Close();
                    software.Dispose();

                    Logger.Info("RegistryUtility-WriteByte:path-" + path + ";key-" + key + ";value-" + value + ";写入成功");
                }
            }
            catch (Exception ex)
            {
                Logger.Error("RegistryUtility-WriteByte:path-" + path + ";key-" + key + ";value-" + value + ";错误信息:" + ex.Message);
            }
        }

        /// <summary>
        /// 读取注册表 byte类型
        /// </summary>
        /// <param name="path">路径为software下相对路径</param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static byte ReadByte(string path, string key)
        {
            try
            {
                byte ret = 0;
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey("software\\" + path, true);
                if (software == null)
                {
                    Logger.Warning("RegistryUtility-ReadByte:path-" + path + ";key-" + key + ";路径不存在");
                    return ret;
                }
                if (software.GetValueNames().Contains(key))
                {
                    ret = Convert.ToByte(software.GetValue(key));
                }
                else
                {
                    Logger.Warning("RegistryUtility-ReadByte:path-" + path + ";key-" + key + ";不存在该键");
                    return ret;
                }

                Logger.Info("RegistryUtility-ReadByte:path-" + path + ";key-" + key + ";读取成功");
                return ret;
            }
            catch (Exception ex)
            {
                Logger.Error("RegistryUtility-ReadByte:path-" + path + ";key-" + key + ";错误信息:" + ex.Message);
                return 0;
            }
        }

        /// <summary>
        /// 写注册表 double类型
        /// </summary>
        /// <param name="path">路径为software下相对路径</param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void WriteDouble(string path, string key, double value)
        {
            try
            {
                using (RegistryKey hkml = Registry.LocalMachine)
                {
                    RegistryKey software = hkml.OpenSubKey("software\\" + path, true);
                    if (software == null)
                    {
                        software = hkml.CreateSubKey("software\\" + path, RegistryKeyPermissionCheck.ReadWriteSubTree);
                    }
                    software.SetValue(key, value, RegistryValueKind.QWord);
                    software.Close();
                    software.Dispose();

                    Logger.Info("RegistryUtility-WriteDouble:path-" + path + ";key-" + key + ";value-" + value + ";写入成功");
                }
            }
            catch (Exception ex)
            {
                Logger.Error("RegistryUtility-WriteDouble:path-" + path + ";key-" + key + ";value-" + value + ";错误信息:" + ex.Message);
            }
        }

        /// <summary>
        /// 读取注册表 double类型
        /// </summary>
        /// <param name="path">路径为software下相对路径</param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static double ReadDouble(string path, string key)
        {
            try
            {
                double ret = 0;
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey("software\\" + path, true);
                if (software == null)
                {
                    Logger.Warning("RegistryUtility-ReadDouble:path-" + path + ";key-" + key + ";路径不存在");
                    return ret;
                }
                if (software.GetValueNames().Contains(key))
                {
                    ret = Convert.ToDouble(software.GetValue(key));
                }
                else
                {
                    Logger.Warning("RegistryUtility-ReadDouble:path-" + path + ";key-" + key + ";不存在该键");
                    return ret;
                }

                Logger.Info("RegistryUtility-ReadDouble:path-" + path + ";key-" + key + ";读取成功");
                return ret;
            }
            catch (Exception ex)
            {
                Logger.Error("RegistryUtility-ReadDouble:path-" + path + ";key-" + key + ";错误信息:" + ex.Message);
                return 0;
            }
        }

        /// <summary>
        /// 获取路径下所有键值对
        /// </summary>
        /// <param name="path">路径为software下相对路径</param>
        /// <returns></returns>
        public static Dictionary<string, string> GetKeyValueList(string path)
        {
            try
            {
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey("software\\" + path, true);
                if (software == null)
                {
                    Logger.Warning("RegistryUtility-GetValueList:path-" + path + ";路径不存在");
                    return null;
                }
                Dictionary<string, string> dc = new Dictionary<string, string>();
                foreach (string vn in software.GetValueNames())
                {
                    dc.Add(vn, software.GetValue(vn).ToString());
                }
                return dc;
            }
            catch (Exception ex)
            {
                Logger.Error("RegistryUtility-GetValueList:path-" + path + ";错误信息:" + ex.Message);
                return null;
            }
        }
    }
}
